import {request} from '../../utils/http'

Page({
  data: {
    couponId:null,
    info:{
      user_rules:[]
    },
    member:{
      mobile:''
    }
  },
  onLoad(query:any) {
    let options = my.getEnterOptionsSync();
    console.log(options, query)
    this.setData({
      couponId:options.query.couponId || query.couponId
    }, () => {
      this.getInfo()
    })
  },
  onShow(){
    my.getStorage({
      key: 'userInfo',
      success: (res) => {
        console.log(res)
        if (res.data) {
          this.setData({
            member: res.data,
          })
        }
      }
    });
  },
  getInfo(){
    request(`/coupon/${this.data.couponId}`,'get').then(res => {
      console.log(res)
      if (res.code == 200) {
        this.setData({
          info:res.data
        })
      }
    })
  },
  receive(){
    let mobile_login = my.getStorageSync({key:'mobile_login'}).data
    console.log(mobile_login)
    if (!this.data.member.mobile) {
      if (mobile_login == 1) {
        my.navigateTo({
          url:'/pages/login-by-phone/login-by-phone'
        })
      } else {
        my.navigateTo({
          url:'/pages/login/login'
        })
      }
     
      return;
    }
    request(`/coupon_get/${this.data.couponId}`,'get').then(res => {
      if (res.code == 200) {
        my.navigateTo({
          url:'/pages/coupon-result/coupon-result?status=70'
        })
      } else {
        my.showToast({
          content: res.message
        })
       
      }
    })
  },
  goCouponList(){
    let mobile_login = my.getStorageSync({key:'mobile_login'}).data
    if (!this.data.member.mobile) {
      if (mobile_login == 1) {
        my.navigateTo({
          url:'/pages/login-by-phone/login-by-phone'
        })
      } else {
        my.navigateTo({
          url:'/pages/login/login'
        })
      }
     
      return;
    }

    my.navigateTo({
      url:'/pages/coupon-list/coupon-list'
    })
  }
});
